package com.tairanchina.md.elife.rabbit.client;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/** Rabbit客户端
 * Created by lxliuxuan on 2016/12/16.
 */
public abstract  class RabbitClient {
    private Logger logger = LoggerFactory.getLogger(RabbitClient.class);
    //rabbitMQ连接工厂
    protected ConnectionFactory factory;


    //rabbitMQ连接，
    protected Connection connection;

    //主机地址
    protected  String host;

    //用户名
    protected  String username;

    //密码
    protected  String password;

    //虚拟主机
    protected  String virtualhost;

    //端口号
    protected  Integer port;


    public RabbitClient() {

    }

    /**
     * 获取连接通道
     * @return
     * @throws Exception
     */
    public abstract Channel getChannel() throws Exception;

    /**
     * 初始化连接
     */
    public abstract void initConnection() ;

    /**
     * 初始化RabbitMQ
     */
    public abstract void init() ;

    /**
     * 初始化连接工厂
     */
    public abstract void initConnectionFactory() ;

    /**
     * 获取连接工厂
     * @return
     */
    public abstract ConnectionFactory getConnectionFactory();

    /**
     * 获取连接
     * @return
     */
    public  abstract  Connection getConnection();


    /**
     * 初始化路由 路由绑定队列
     * @param exchangeName    路由名称 ( 可以理解为队列类型, 建议每一个业务至少一个路由 )
     * @param exchangeType    路由类型
     * @param queueNames      队列名称
     * @param queueKeys       队列key ( 每一个队列可以拥有0-1个key,
     *                                不同队列可以使用同样的key,
     *                                同一路由下使用相同key的队列会收到相同消息 )
     */
    public void initExchange(String exchangeName, String exchangeType, String[] queueNames, String[] queueKeys)
    {
        Channel channel = null;
        try
        {
            channel = connection.createChannel();

            //初始化路由
            channel.exchangeDeclare(exchangeName, exchangeType, true);

            for (int i = 0; i < queueNames.length; i++)
            {

                //路由绑定队列
                channel.queueDeclare(queueNames[i], true, false, false, null);
                channel.queueBind(queueNames[i], exchangeName, queueKeys[i]);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            logger.error("初始化路由失败。e:{}", e);
        }
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVirtualhost() {
        return virtualhost;
    }

    public void setVirtualhost(String virtualhost) {
        this.virtualhost = virtualhost;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}
